package admin.models;

import helpers.GeneralHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import com.google.appengine.api.NamespaceManager;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyOpts;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

import entities.AutoSite;

public class AutoSiteModel {
	private Objectify ofy;
	
	private Cache cache = null;
	
	private String prefix = "auto_site_";
	
	private static AutoSiteModel instance = null;

	public static AutoSiteModel getInstance() {
		if (instance == null) {
			instance = new AutoSiteModel();
		}
		
		return instance;
	}
	
	private AutoSiteModel() {
		try {
			ObjectifyService.register(AutoSite.class);
		} catch (Exception e) {
			//e.printStackTrace();
		}
		
		cache = GeneralHelper.getCache();
		ofy = GeneralHelper.getObjectify();
	}
	
	public List<AutoSite> list(String ns, int page, int limit) {
		String currentNs = NamespaceManager.get();
		if (!currentNs.equals(ns)) {
			NamespaceManager.set(ns);
		}
		
		List<AutoSite> sites = new ArrayList<AutoSite>();
		
		int start = (page - 1) * limit;
		sites = ofy.query(AutoSite.class).limit(limit).offset(start).list();
		
		NamespaceManager.set(currentNs);
		
		return sites;
	}
	
	public List<AutoSite> getAll(String ns) {
		String currentNs = NamespaceManager.get();
		if (!currentNs.equals(ns)) {
			NamespaceManager.set(ns);
		}
		
		List<AutoSite> sites = new ArrayList<AutoSite>();
		
		String cacheName = prefix + "_all_site";
		boolean isCached = false;
		
		if (cache != null) {
			try {
				sites = (List<AutoSite>) cache.get(cacheName);
				if (sites != null) isCached = true;
			} catch (Exception e) {
			}
			
		}
		
		if (isCached)
			System.out.println("cached get all");
		
		if (!isCached) {
			sites = ofy.query(AutoSite.class).list();
			
			if (cache != null) {
				try {
					cache.put(cacheName, sites);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}
		
		NamespaceManager.set(currentNs);
		
		return sites;
	}
	
	public void insert(String ns, AutoSite site) {
		String currentNs = NamespaceManager.get();
		if (!currentNs.equals(ns)) {
			NamespaceManager.set(ns);
		}
		
		try {
			String cacheName = prefix + "_all_site";
			cache.remove(cacheName);
		} catch (Exception e) {}
		
		ofy.put(site);
		
		NamespaceManager.set(currentNs);
	}
	
	public void delete(String ns, AutoSite site) {
		String currentNs = NamespaceManager.get();
		if (!currentNs.equals(ns)) {
			NamespaceManager.set(ns);
		}
		
		try {
			String cacheName = prefix + "_all_site";
			cache.remove(cacheName);
		} catch (Exception e) {}
		
		ofy.delete(site);
		
		NamespaceManager.set(currentNs);
	}
	
	public AutoSite getSite(String ns, String id) {
		String currentNs = NamespaceManager.get();
		if (!currentNs.equals(ns)) {
			NamespaceManager.set(ns);
		}
		
		AutoSite site = new AutoSite();
		try {
			site = ofy.get(new Key<AutoSite>(AutoSite.class, id));
		} catch (Exception e) {
			site = null;
		}
		
		NamespaceManager.set(currentNs);
		
		return site;
	}
}
